package org.wmq.travelagency02.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.RouteMatcher;
import org.wmq.travelagency02.mapper.RouteMapper;
import org.wmq.travelagency02.model.Entity.Route;
import org.wmq.travelagency02.service.RouteService;

import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
public class RouteServiceImpl implements RouteService {
    private final RouteMapper routeMapper;

    @Override
    public Route getById(Long id) {
        Optional<Route> option = this.routeMapper.findById(id);
        if (option.isEmpty()){
            return null;
        }
        return option.get();
    }

    @Override
    public void save(Route route) {
        this.routeMapper.save(route);
    }

    @Override
    public boolean updateById(Route route) {
        route = this.routeMapper.save(route);
        return route != null;
    }

    @Override
    public boolean removeById(Long id) {
        this.routeMapper.deleteById(id);
        return true;
    }

    @Override
    public List<Route> list() {
        return this.routeMapper.findAll();
    }

    @Override
    public List<Route> page(Long pageNo, Long pageSize) {
        Page<Route> result = this.routeMapper.findAll(PageRequest.of(pageNo.intValue(), pageSize.intValue()));
        return result.getContent();
    }
}
